package com.abner.activiti.query;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.task.Task;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 删除流程
 * @author: guoc
 * @createDate: 2020/9/11 11:02
 *      * act_ge_bytearray     部署信息
 *      * act_re_deployment    流程定义的信息
 *      * act_re_procdef       流程定义的bpmn&png文件
 *使用 repositoryService 删除流程定义
 *如果该流程定义下没有正在运行的流程，则可以用普通删除。
 *如果该流程定义下存在已经运行的流程，使用普通删除报错，可用级联删除方法将流程及相关
 * 记录全部删除。项目开发中使用级联删除的情况比较多，删除操作一般只开放给超级管理员使
 * 用
 */
public class activitiDeleteDeployment {



    /**
     * 方法描述:获取部署ID
     * @return java.lang.String
     * @author guoc
     * @date 2020/9/11 11:40
     */
    public String  queryProceccDefinition(ProcessEngine defaultProcessEngine){
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        ProcessDefinition game = processDefinitionQuery.processDefinitionKey("game").singleResult();
        return game.getDeploymentId();
    }

    /**
     * 方法描述:获取部署IDlist
     * @return java.util.List<java.lang.String>
     * @author guoc
     * @date 2020/9/11 13:38
     */
    public List<String>  queryProceccDefinitionList(ProcessEngine defaultProcessEngine){
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> game = processDefinitionQuery.processDefinitionKey("game").orderByProcessDefinitionVersion().desc().list();
        List<String> definitionList = new ArrayList<>();
        for (ProcessDefinition processDefinition : game) {
            definitionList.add(processDefinition.getDeploymentId());
        }
        return definitionList;
    }


    /**
     * 删除定义的流程
     * @return void
     * @author guoc
     * @date 2020/9/11 11:44
     */
    public void deleteDeployment(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        String DeploymentId = queryProceccDefinition(defaultProcessEngine);
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        if (DeploymentId.equals("1")){
            repositoryService.deleteDeployment(DeploymentId);
        }

    }

    /**
     * 级联强制删除
     * @return void
     * @author guoc
     * @date 2020/9/11 13:45
     */
    public void deleteDeployments(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        List<String> strings = queryProceccDefinitionList(defaultProcessEngine);
        for (String string : strings) {
            repositoryService.deleteDeployment(string,true);
        }
    }



    public static void main(String[] args) {
        activitiDeleteDeployment activitiDeleteDeployment=new activitiDeleteDeployment();
        //activitiDeleteDeployment.deleteDeployment();
        activitiDeleteDeployment.deleteDeployments();

    }
}
